<?php
// 测试食材匹配逻辑的脚本

// 数据库配置
$host = '127.0.0.1';
$dbname = 'yingyang';
$username = 'root';
$password = 'root';
$charset = 'utf8mb4';

try {
    $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 测试食材匹配逻辑 ===\n\n";
    
    // 测试一些常见的食材名称
    $testIngredients = [
        '木耳',
        '大蒜',
        '盐',
        '西兰花',
        '姜片',
        '料酒',
        '白菜',
        '醋'
    ];
    
    foreach ($testIngredients as $ingredientName) {
        echo "测试食材: $ingredientName\n";
        
        // 精确匹配
        $stmt = $pdo->prepare('SELECT id, name, parent_id, categories_id FROM fa_food_ingredient WHERE name = ?');
        $stmt->execute([$ingredientName]);
        $exactMatch = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($exactMatch) {
            echo "  ✓ 精确匹配: ID={$exactMatch['id']}, 名称={$exactMatch['name']}\n";
        } else {
            echo "  ✗ 无精确匹配\n";
            
            // 模糊匹配
            $stmt = $pdo->prepare('SELECT id, name, parent_id, categories_id FROM fa_food_ingredient WHERE name LIKE ? ORDER BY id LIMIT 1');
            $stmt->execute(["%$ingredientName%"]);
            $fuzzyMatch = $stmt->fetch(PDO::FETCH_ASSOC);
            
            if ($fuzzyMatch) {
                echo "  ~ 模糊匹配: ID={$fuzzyMatch['id']}, 名称={$fuzzyMatch['name']}\n";
            } else {
                echo "  ✗ 无模糊匹配\n";
            }
        }
        echo "\n";
    }
    
} catch (Exception $e) {
    echo "执行失败: " . $e->getMessage() . "\n";
}
?>